package logic

import (
	"context"
	"fmt"
	"user/models/mysqlinit"
	"user/pkg"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *userrpc.LoginRequest) (*userrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var login mysqlinit.User
	mysqlinit.DB.Debug().Where("account=?", in.Account).Find(&login)
	if login.ID == 0 {
		return nil, fmt.Errorf("账户错误，请前往注册")
	}
	if login.Password != pkg.Md5Password(in.Password) {
		return nil, fmt.Errorf("密码错误，请重新输入")
	}

	return &userrpc.LoginResponse{
		Id: int64(login.ID),
	}, nil
}
